﻿/* $RCSFile: EditProfile.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/Secure2/EditProfile.aspx.cs $
 * $log$
 * Revision 27 2011/01/21 17:51:08  christian.surieux
 *   Recover from Sony move files updated in 11/2010 were not in Source Control
 * Revision 20 2010/08/09 10:51:22  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:44:01  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Threading;
using System.Globalization;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Drawing;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Xml;
using System.Web.Security;

using AddonNice.Settings;
using AddonNice.Authentication;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Core;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Country;
using AddonNice.CRMWrapper;

using Telerik.Web.UI;

using Trace2 = System.Diagnostics.Trace;

namespace AddonNice.Secure
{
    public partial class EditProfile : StaticStdAuxPage
    {
        // Must be const PageEditProfileID
        public override int PageID
		{
			get
			{   return StStr.PageEditProfileID; }
		}

        public EditProfile() : base(true,true,true,false)
        {
            DefaultUrlReferrerToPageID  =   false;
            ValidationGroup             =   "Main";
        }
        

        private AddonNiceUser _currentUser = null;
        private AddonNiceUser currentUser
        {
            get
            {

                if ( PageTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine("[V]EditProfile currentUser GET BEG.",PageTraceSwitch.Sw.Info);

                if (_currentUser == null)
                {
                    try
                    {
                        _currentUser    = new AddonNiceUser();
                    }
                    catch (Exception ex)
                    {
                        if ( PageTraceSwitch.Sw.TraceError )
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile currentUser GET ex: {0}", ex),PageTraceSwitch.Sw.Info);
                        throw;
                    }
                }
                if (_currentUser == null)
                {
                    const string msg = "[E]EditProfile currentUser is NULL";
                    if ( PageTraceSwitch.Sw.TraceError )
                        Trace2.WriteLine(msg,PageTraceSwitch.Sw.Info);
                    throw new Exception(msg);
                }
                if ( PageTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile currentUser Get Key: [{0}],userName: [{1}]",
                         _currentUser.ProviderUserKey, _currentUser.UserName),PageTraceSwitch.Sw.Info);
                return _currentUser;

            }
        }

        
        /// <summary>
        /// The billing from country : could be not installed at that time
        /// </summary>
		private string BillCountry
		{
			get
			{	
				if ( ViewState["BCtr"] != null )
					return (string)ViewState["BCtr"];
				return string.Empty;
			}
			set
			{	
				ViewState["BCtr"]	=	value;
			}
		}
		
		private string BillState
		{
			get
			{	
				if ( ViewState["BSte"] != null )
					return (string)ViewState["BSte"];
				return string.Empty;
			}
			set
			{	
				ViewState["BSte"]	=	value;
			}
		}

		private string ShipCountry
		{
			get
			{	
				if ( ViewState["SCtr"] != null )
					return (string)ViewState["SCtr"];
				return string.Empty;
			}
			set
			{	
				ViewState["SCtr"]	=	value;
			}
		}
		
		private string ShipState
		{
			get
			{	
				if ( ViewState["SSte"] != null )
					return (string)ViewState["SSte"];
				return string.Empty;
			}
			set
			{	
				ViewState["SSte"]	=	value;
			}
		}

        /// <summary>
        /// Check main fields not empty
        /// </summary>
        bool ShippingAddressOk
        {
            get
            {

                return (  !string.IsNullOrEmpty(ShipCityField.Text.Trim()) &&
                         !string.IsNullOrEmpty(ShipAddressField1.Text.Trim()) &&
                        ( RadShipCountryField.SelectedIndex != -1 ) &&
                         !string.IsNullOrEmpty(ShipZipField.Text.Trim()) );
            }
        }

        void Page_Load(object sender, System.EventArgs e)
        {
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile Page_Load BEG IsPostBack: {0}",IsPostBack),SecurityTraceSwitch.Sw.Info);
            if (!UserLogged)
            {
                if ( SecurityTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine("[V]EditProfile Rights checked calling portalhome.",SecurityTraceSwitch.Sw.Info);
                PortalSecurity.PortalHome(true,this);
            }
			if ( !IsPostBack ) 
			{
				try
				{
                    ResetUserInfoFromUser();
				}
				catch(Exception ex)
				{
					if ( SecurityTraceSwitch.Sw.TraceError )
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile Page_Load ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
					Exit();
				}
			}
        }

		#region State and Country
		
        void RadCountryField_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
			if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile RadCountryField_SelectedIndexChanged BEG e.Text: {0}",e.Text),SecurityTraceSwitch.Sw.Info);
			try
			{
				BillCountry					=	e.Value;
				BillState					=	string.Empty;
                RadStateField.ClearSelection();
				RadStateField.Items.Clear();
				BindState();
			}
			catch(Exception ex)
			{
				if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile RadCountryField_SelectedIndexChanged ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
                throw;
			}
			if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile RadCountryField_SelectedIndexChanged END BillCountry: '{0}'",BillCountry),SecurityTraceSwitch.Sw.Info);
		}

		private void SCountryField_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
		{
			if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile SCountryField_SelectedIndexChanged BEG e.Text: {0}",e.Text),SecurityTraceSwitch.Sw.Info);
			try
			{
				ShipCountry					=	e.Value;
				ShipState					=	string.Empty;
                RadShipStateField.Items.Clear();
				ShipBindState();
			}
			catch(Exception ex)
			{
				if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile SCountryField_SelectedIndexChanged: {0}",ex),SecurityTraceSwitch.Sw.Info);
                throw;
			}
			if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile SCountryField_SelectedIndexChanged END ShipCountry: '{0}'",ShipCountry),SecurityTraceSwitch.Sw.Info);
		}

		/// <summary>
		/// Create a list of countries where we accept shipping: TBD we must check that each order is limited to one of these countries
        /// If user shipping coutries is amonst them, we should not accept orders from him
		/// </summary>
        private void BindCountry()
		{
			try
			{
                //Country filter limits country list
                string CountriesFilter              =   (Config.AuthenticationMode == AuthenticationMode.Forms) ?
                    portalSettings.CustomSettings[StStr.SiteSettingsCountryFilter].ToString():string.Empty;
                if (SecurityTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile BindCountry BEG CountriesFilter: '{0}'",CountriesFilter),SecurityTraceSwitch.Sw.Info);
                RadCountryField.DataValueField      =   "ThreeLetterISOCountryName";
                RadCountryField.DataTextField       =   "DisplayName";
                if ( CountriesFilter != string.Empty )
				{
					RadCountryField.DataSource      =   CountryInfo.GetCountries(CountriesFilter, CountryFields.DisplayName);
				}
				else /// buyer from anywhere
				{
					RadCountryField.DataSource      =   CountryInfo.GetCountries(CountryTypes.InhabitedCountries,CountryFields.DisplayName);
				}
				RadCountryField.DataBind();
                RadStateField.Items.Clear();
			    if ( SecurityTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile BindCountry BillCountry: '{0}'",BillCountry),SecurityTraceSwitch.Sw.Info);

                if (RadCountryField.Items.Count > 0)
                {
                    if (string.IsNullOrEmpty(BillCountry) )
                    {
                        BillCountry                 =   RadCountryField.Items[0].Value;
                        if (SecurityTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile BindCountry BillCountry: {0}", BillCountry), SecurityTraceSwitch.Sw.Info);
                    }
                    RadCountryField.SelectedValue   =   BillCountry;
				    BindState();
                }
			}
			catch(Exception ex)
			{
				if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile BindCountry ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
				throw;
			}
		}

		private void BindState()
		{
			BindState2();
		}
		
		private void BindState2()
		{
			try
			{
				string currentCountry				=   BillCountry;
			    if ( SecurityTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile BindState BEG StrState: '{0}', currentCountry: '{1}'",
                        BillState,currentCountry),SecurityTraceSwitch.Sw.Info);
				if ( currentCountry != string.Empty )
				{
                    RadStateField.DataValueField    =   "EnglishName";
                    RadStateField.DataTextField     =   "EnglishName";
					RadStateField.DataSource        =	new CountryInfo(currentCountry).Childs;
					RadStateField.DataBind();
					if ( RadStateField.Items.Count > 0 )
					{
                        if (BillState == string.Empty)
                        {
                            BillState               =   RadStateField.Items[0].Value;
                        }
                        RadStateField.SelectedValue =   BillState;
					}
				}
			}
			catch(Exception ex)
			{
				if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile BindState ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
				throw;
			}
		}

        /// <summary>
        /// From allowed crountries bind the shipContries and shipStates combobox
        /// </summary>
        private void ShipBindCountry()
		{
			try
			{
				//Country filter limits country list
                string CountriesFilter              =   (Config.AuthenticationMode == AuthenticationMode.Forms) ?
                    portalSettings.CustomSettings[StStr.SiteSettingsCountryFilter].ToString():string.Empty;

                RadShipCountryField.DataValueField  =   "ThreeLetterISOCountryName";
                RadShipCountryField.DataTextField   =   "DisplayName";
				if ( CountriesFilter != string.Empty )
				{
					RadShipCountryField.DataSource  =   CountryInfo.GetCountries(CountriesFilter);
				}
				else
				{
					RadShipCountryField.DataSource	=   CountryInfo.GetCountries(CountryTypes.InhabitedCountries,CountryFields.DisplayName);
				}
				RadShipCountryField.DataBind();
				RadShipStateField.Items.Clear();
                if ( RadShipCountryField.Items.Count > 0)
                {
                    if (string.IsNullOrEmpty(ShipCountry) )
                    {
                        ShipCountry                 =   RadShipCountryField.Items[0].Value;
                        if (SecurityTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile ShipBindCountry set ShipCountry: {0}", ShipCountry), SecurityTraceSwitch.Sw.Info);
                    }

					RadShipCountryField.SelectedValue   =   ShipCountry;
				    ShipBindState();
                }
			}
			catch(Exception ex)
			{
				if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile ShipBindCountry ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
				throw;
			}
		}

		private void ShipBindState()
		{
			if ( string.IsNullOrEmpty(ShipState) )
			{
				if ( ShipCountry	==	 BillCountry )
				{
					if ( RadStateField.SelectedItem != null )
						ShipState   =   RadStateField.SelectedValue;
				}
			}
			ShipBindState2();
		}
		
		private void ShipBindState2()
		{
			try
			{
				string currentShipCountry						=	ShipCountry;
			    if ( SecurityTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile ShipBindState BEG StrState: '{0}', currentCountry: '{1}'",
                        ShipState,currentShipCountry),SecurityTraceSwitch.Sw.Info);
				if ( currentShipCountry != string.Empty )
				{
                    RadShipStateField.DataValueField            =   "EnglishName";
                    RadShipStateField.DataTextField             =   "EnglishName";
					RadShipStateField.DataSource			    =	new CountryInfo(currentShipCountry).Childs;
					RadShipStateField.DataBind();
					if ( RadShipStateField.Items.Count > 0  )
					{
                        if (ShipState == string.Empty)
                        {
                            ShipState                           =   RadShipStateField.Items[0].Value;
                        }
						RadShipStateField.SelectedValue         =   ShipState;
					}
				}
			}
			catch(Exception ex)
			{
				if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile ShipBindState ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
				throw;
			}
		}
		
        #endregion

        #region Copy Shipping
		private void CopyShipping()
		{
			// copy all textFields
			ShipAddressField1.Text	=	AddressField1.Text;
			ShipAddressField2.Text	=	AddressField2.Text;
			ShipAddressField3.Text	=	AddressField3.Text;
			ShipCityField.Text		=	CityField.Text;
			ShipZipField.Text		=	ZipField.Text;
			ShipPhoneField.Text		=	PhoneField.Text;
			ShipFaxField.Text		=	FaxField.Text;
			// we reset LB to Billing values
			ShipCountry				=	(RadCountryField.SelectedIndex != -1 ) ? RadCountryField.SelectedValue:string.Empty;
            // anyway we reset shipstates
            if ( RadShipStateField.Items.Count > 0 )
                RadShipStateField.Items.Clear();
            if ( string.IsNullOrEmpty(BillState) )
			{
				ShipState		    =	string.Empty;
			}
			else
			{
				ShipState			=	BillState;
			}
			try 
			{ 
				ShipBindCountry();
			}
			catch(Exception ex)
			{
				if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile CopyShipping ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
				throw;
			}
		}

		private void CopyShippingBtn_Click(object sender, System.EventArgs e)
		{
			try
			{
				CopyShipping();
			}
			catch(Exception ex)
			{
				if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile CopyShippingBtn_Click ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
                throw;
			}

		}
		#endregion

		#region Billing Info

		private void ResetUserInfoFromUser()
		{
	        if ( SecurityTraceSwitch.Sw.TraceVerbose )
	            Trace2.WriteLine("[V]EditProfile ResetUserInfoFromUser BEG.",SecurityTraceSwitch.Sw.Info);
			FirstNameField.Text	=	currentUser.UserFirstName;
            NicknameField.Text  =   currentUser.nickname;
			NameField.Text		=	currentUser.UserName;
			EmailField.Text		=	currentUser.Email;
            Question.Text       =   currentUser.PasswordQuestion;
            CompanyField.Text   =   currentUser.Company;
            SicField.Text       =   currentUser.Sic;
            
            // we enter pwd only when in Forms authentication
            rowPwd.Visible      =   currentUser.hasPassword;
            // we display company row only when managing parentaccoundid in provider
            if ( !currentUser.hasCompany )
            {
                CompanyRow.Visible  =   false;
                SicRow.Visible      =   false;
            }
            
            AddressField1.Text  =   currentUser.BillAddress1;
			AddressField2.Text	=	currentUser.BillAddress2;
			AddressField3.Text	=	currentUser.BillAddress3;
			ZipField.Text		=	currentUser.BillZip;
			CityField.Text		=	currentUser.BillCity;
            if (!string.IsNullOrEmpty(currentUser.BillCountry))
            {
                BillCountry     =   currentUser.BillCountryID;
                if (!string.IsNullOrEmpty(currentUser.BillCounty))
			        BillState	=	currentUser.BillCounty;
            }
			
            FaxField.Text		=	currentUser.BillFax;
			PhoneField.Text		=	currentUser.BillPhone;
			
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile ResetUserInfoFromUser AddressField1: {0}, ZipField: {1}, CityField: {2}",
                    AddressField1.Text,ZipField.Text,CityField.Text),SecurityTraceSwitch.Sw.Info);
            
            try
			{
				BindCountry();
			}
			catch(Exception ex)
			{
		        if ( SecurityTraceSwitch.Sw.TraceError )
		            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile ResetUserInfoFromUser BindCountry ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
				throw;
			}

			ShipAddressField1.Text	=	currentUser.ShipAddress1;
			ShipAddressField2.Text	=	currentUser.ShipAddress2;
			ShipAddressField3.Text	=	currentUser.ShipAddress3;
			ShipZipField.Text		=	currentUser.ShipZip;
			ShipCityField.Text		=	currentUser.ShipCity;
			ShipFaxField.Text		=	currentUser.ShipFax;
			ShipPhoneField.Text		=	currentUser.ShipPhone;
            
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile ResetUserInfoFromUser ShipAddressField1: {0}, ShipZipField: {1}, ShipCityField: {2}",
                    ShipAddressField1.Text,ShipZipField.Text,ShipCityField.Text),SecurityTraceSwitch.Sw.Info);
            // if shipping address is not Ok, copy billing
            if (string.IsNullOrEmpty(currentUser.ShipAddress1) ||
                string.IsNullOrEmpty(currentUser.ShipZip) ||
                string.IsNullOrEmpty(currentUser.ShipCity))
            {
                CopyShipping();
            }
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile ResetUserInfoFromUser after copy ShipAddressField1: {0}, ShipZipField: {1}, ShipCityField: {2}",
                    ShipAddressField1.Text,ShipZipField.Text,ShipCityField.Text),SecurityTraceSwitch.Sw.Info);
            
            if ( !string.IsNullOrEmpty(currentUser.ShipCountryID) ||
                !string.IsNullOrEmpty(currentUser.BillCountryID))
            {
                ShipCountry         =   string.IsNullOrEmpty(currentUser.ShipCountryID) ? currentUser.BillCountryID:currentUser.ShipCountryID;
                if (!string.IsNullOrEmpty(currentUser.ShipCounty) ||
                    ( currentUser.BillCountry.Equals(currentUser.ShipCountry ) && !string.IsNullOrEmpty(currentUser.BillCounty) ))
                    ShipState = string.IsNullOrEmpty(currentUser.ShipCounty) ? currentUser.BillCounty : currentUser.ShipCounty;
            }
            try
			{
				ShipBindCountry();
			}
			catch(Exception ex)
			{
		        if ( SecurityTraceSwitch.Sw.TraceError )
		            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile ResetUserInfoFromUser ShipBindCountry ex: {0}",ex),SecurityTraceSwitch.Sw.Info);
				throw;
			}
        }

        #endregion


        #region Exit

        private void Exit()
        {
            // stay in the secure area, confirm leaving secure site
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]EditProfile Exit BEG.",SecurityTraceSwitch.Sw.Info);
            try
            {
                RedirectBackToReferringPage();
            }
            catch (ThreadAbortException )
            {
                throw;
            }
            catch (Exception ex)
            {
                if ( SecurityTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile Exit failed ex: {0}", ex),SecurityTraceSwitch.Sw.Info);
                throw;
            }
        }

        #endregion

        #region update profile

        void EditProfile_Update(object sender, ADNPageEventArgs e)
        {
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]EditProfile EditProfile_Update BEG.",SecurityTraceSwitch.Sw.Info);
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile EditProfile_Update CaptchaControl1.UserValidated: {0}.",RadCaptcha1.IsValid),SecurityTraceSwitch.Sw.Info);
            
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile EditProfile_Update rowPwd.Visible: {0}, ChkChangePwd.Checked: {1}.",rowPwd.Visible,ChkChangePwd.Checked),SecurityTraceSwitch.Sw.Info);
            // check when pwd is asked to be changed
            if ( rowPwd.Visible && ChkChangePwd.Checked )
            {
                string newpwd           =   Password.Text.Trim();
                if (!currentUser.CheckPassword(newpwd))
                {
                    LblErr.Text             =   LocalizeHelper.Localize("PROFILE_PWD_ERR","Incorrect Password");
                    e.Cancel                =   true;
                    return;
                }
                if ((Question.Text.Trim() == string.Empty) || ( Answer.Text.Trim() == string.Empty))
                {
                    LblErr.Text             =   LocalizeHelper.Localize("PROFILE_QUESTION_ERR","Question / Answer empty");
                    e.Cancel                =   true;
                    return;
                }

                currentUser.Question        =   Question.Text.Trim();
                currentUser.Answer          =   Answer.Text.Trim();
                currentUser.NewPwd          =   newpwd;
            }
            currentUser.UserFirstName       =   FirstNameField.Text;
            currentUser.nickname            =   NicknameField.Text;
            currentUser.UserName            =   NameField.Text;
            currentUser.Email               =   EmailField.Text;
            currentUser.Company             =   CompanyField.Text;
            currentUser.Sic                 =   SicField.Text;
            currentUser.BillAddress1        =   AddressField1.Text;
            currentUser.BillAddress2        =   AddressField2.Text;
            currentUser.BillAddress3        =   AddressField3.Text;
            currentUser.BillZip             =   ZipField.Text;
            currentUser.BillCity            =   CityField.Text;
            // we must take name and not displayname because we are unable to search on displayname
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile EditProfile_Update RadCountryField.SelectedValue: {0}.",RadCountryField.SelectedValue.ToString()),SecurityTraceSwitch.Sw.Info);

            if (!string.IsNullOrEmpty(RadCountryField.SelectedValue))
            {
                CountryInfo ci              =   new CountryInfo(RadCountryField.SelectedValue);
                currentUser.BillCountry     =   ci.EnglishName;
                if (!string.IsNullOrEmpty(RadStateField.SelectedValue))
                {
                    currentUser.BillCounty  =   RadStateField.SelectedValue;
                }
                else
                {
                    currentUser.BillCounty  =   string.Empty;
                }
            }
            else
            {
                currentUser.BillCountry     =   string.Empty;
                currentUser.BillCounty      =   string.Empty;
            }

            currentUser.BillFax             =   FaxField.Text;
            currentUser.BillPhone           =   PhoneField.Text;

            currentUser.ShipAddress1        =   ShipAddressField1.Text;
            currentUser.ShipAddress2        =   ShipAddressField2.Text ;
            currentUser.ShipAddress3        =   ShipAddressField3.Text ;
            currentUser.ShipZip             =   ShipZipField.Text ;
            currentUser.ShipCity            =   ShipCityField.Text ;
            currentUser.ShipFax             =   ShipFaxField.Text ;
            currentUser.ShipPhone           =   ShipPhoneField.Text ;
            // we must take name and not displayname because we are unable to search on displayname
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditProfile EditProfile_Update RadShipCountryField.SelectedValue: {0}.",RadShipCountryField.SelectedValue.ToString()),SecurityTraceSwitch.Sw.Info);
            if (!string.IsNullOrEmpty(RadShipCountryField.SelectedValue))
            {
                CountryInfo ci              =   new CountryInfo(RadShipCountryField.SelectedValue);
                currentUser.ShipCountry     =   ci.EnglishName;
                if (!string.IsNullOrEmpty(RadShipStateField.SelectedValue))
                {
                    currentUser.ShipCounty  =   RadShipStateField.SelectedValue;
                }
                else
                {
                    currentUser.ShipCounty  =   string.Empty;
                }
            }
            else
            {
                currentUser.ShipCounty      =   string.Empty;
                currentUser.ShipCountry     =   string.Empty;
            }

            try
            {
                currentUser.Update();
            }
            catch (Exception ex)
            {
                if (PageTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EditProfile update ex: {0}", ex), PageTraceSwitch.Sw.Info);
                throw;
            }
            if ( SecurityTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]EditProfile EditProfile_Update END.",SecurityTraceSwitch.Sw.Info);
        }

        #endregion update profile

        #region Std overrides

        /// <summary>
        /// Buttons localization
        /// </summary>
        protected override void OnPreInit(EventArgs e)
        {
            MasterPage myMaster         =   Master; // instanciate placeholders from master
            //string strcult              =   LocalizeHelper.GetCurrentNeutralCultureName();
            //Panel2CancelBtn.SkinID      =   "cancel_" + strcult;
            base.OnPreInit(e);
        }

        override protected void OnInit(EventArgs e)
        {
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]EditProfile OnInit BEG", PageTraceSwitch.Sw.Info);
            InitializeComponent();
            base.OnInit(e);
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]EditProfile OnInit END", PageTraceSwitch.Sw.Info);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            RadCountryField.SelectedIndexChanged    +=  new RadComboBoxSelectedIndexChangedEventHandler(RadCountryField_SelectedIndexChanged);
            RadCountryField.AutoPostBack            =   true;
 
            CopyShippingBtn.Click                   +=  new EventHandler(CopyShippingBtn_Click);
			
            RadShipCountryField.SelectedIndexChanged +=    new RadComboBoxSelectedIndexChangedEventHandler(SCountryField_SelectedIndexChanged);
            RadShipCountryField.AutoPostBack        =   true;

            Load                                    +=  new EventHandler(Page_Load);
            Update                                  +=  new PageEventHandler(EditProfile_Update);
        }


        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
        {
            get { return PlaceHolderButtons; }
            set { }
        }

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
        {
            get { return PlaceHolderButtons2; }
            set { }
        }
        #endregion
    }
}